'use strict';

const { join } = require('path');
const { readdirSync } = require('fs');
const execa = require('execa');
const inquirer = require('inquirer');

const packages = join(__dirname, '..', 'packages');

inquirer
  .prompt([{
    name: 'name',
    message: '请选择需要发布的模块',
    type: 'checkbox',
    choices: readdirSync(packages)
  }, {
    name: 'tag',
    message: '请选择需要发布的tag',
    type: 'list',
    choices: [
      { name: 'latest', value: [] },
      { name: 'beta', value: ['--tag', 'beta'] }
    ]
  }]).then(({ name, tag }) => {
    name.forEach(async (n) => {
      const args = [
        'publish',
        join(__dirname, '..', 'packages', n),
        ...tag
      ];
      const { stdout, stderr } = await execa('npm', args);
      console.log(stderr);
      console.log(stdout);
    });
  });
